/*
  The MIT License
  
  Copyright (c) 2010 smath-view project
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
package pl.smath.renderer.model;

import pl.smath.renderer.model.sign.MultiplicationSign;
import pl.smath.renderer.renderer.TermRendererException;
import pl.smath.renderer.utils.Cursor;
import pl.smath.renderer.utils.Rectangle;
import gwt.g2d.client.graphics.Surface;

/**
 * ProductView - represents rendered product expression.
 * 
 * @author Damian Rodziewicz
 *
 */
public class ProductView extends AbstractMultiView {
	private MultiplicationSign multiplicationSign;
	/**
	 * Constructor
	 */
	public ProductView() {
		super("*");
		multiplicationSign = new MultiplicationSign();
	}
	
	/**
	 * render text at the current position
	 * @param canvas
	 */
	@Override
	public void render(Surface canvas) throws TermRendererException {
		if ((this.getFont() == null)||(this.getFontHeight() == null)) {
			throw new TermRendererException();
		}
		canvas.setFont(this.getFont());
		
		Cursor cursor = new Cursor(this.getCursor().getX(), this.getCursor().getY() - this.getBaselineOffset());
		
		Double signWidth = this.multiplicationSign.getWidth();
		Double minusWidth = this.minusSign.getWidth();
		Double rightBracketWidth = canvas.measureText(")");
		
		if (this.isNegative()) {
			this.minusSign.render(canvas, cursor);
			cursor.setX(cursor.getX() + minusWidth);
		}
		
		for (int i = 0; i < this.views.size(); i++) {
			View view = this.views.elementAt(i);

			if ((i > 0)&&(!(view instanceof VariableView))) {
				this.multiplicationSign.render(canvas, cursor);
				cursor.setX(cursor.getX() + signWidth);
			}
			
			if(view instanceof SumView) {
				canvas.fillText("(", cursor.getX(), cursor.getY());
			}
			
			view.render(canvas);
			cursor.setX(view.getX() + view.getWidth());

			if(view instanceof SumView) {
				canvas.fillText(")", cursor.getX(), cursor.getY());
				cursor.setX(cursor.getX() + rightBracketWidth);
			}
		}
	}
	

	/**
	 * set the position on canvas to render
	 * @param Cursor
	 */
	@Override
	public void setCursor(Cursor cursor) {
		//if (this.getCursor()==null) {
			super.setCursor(new Cursor(cursor.getX(), cursor.getY()));
			
			Surface canvas = new Surface();
			canvas.setFont(this.getFont());
			
			Double signWidth = this.multiplicationSign.getWidth();
			Double leftBracketWidth = canvas.measureText("(");
			Double rightBracketWidth = canvas.measureText(")");
			Double minusWidth = this.minusSign.getWidth();
			
			Cursor toSet = new Cursor(cursor.getX(), cursor.getY() - this.getBaselineOffset());
			if (this.isNegative()) toSet.setX(toSet.getX() + minusWidth);
			
			for (int i = 0; i < this.views.size(); i++) {
				View view = this.views.elementAt(i);
				
				if ((i > 0)&&(!(view instanceof VariableView))) toSet.setX(toSet.getX() + signWidth);
				
				if(view instanceof SumView) {
					toSet.setX(toSet.getX() + leftBracketWidth);
					view.setCursor(new Cursor(toSet.getX(), toSet.getY() + view.getBaselineOffset()));
					toSet.setX(toSet.getX() + rightBracketWidth);
				} else if (view instanceof FractionView) { 
					Double cursorY = toSet.getY() + ((FractionView) view).getDenominator().getHeight() - this.getFontHeight()/2 + FractionView.getSpace();
					view.setCursor(new Cursor(toSet.getX(), cursorY));
				} else {
					view.setCursor(new Cursor(toSet.getX(), toSet.getY() + view.getBaselineOffset()));
				}
				
				toSet.setX(toSet.getX() + view.getWidth());
			} 
			

		/*} else {
			Double deltaX = cursor.getX() - this.getX();
			Double deltaY = cursor.getY() - this.getY();
			for (View view : this.views) {
				view.setX(view.getX() + deltaX);
				view.setY(view.getY() + deltaY);
			}
			super.setCursor(cursor);
		}*/
		
	}
	
	/**
	 * @param font the font to set
	 */
	@Override
	public void setFont(String font) {
		if (font.indexOf("px")!=-1) {
			this.setFontHeight(new Double(font.substring(0, font.indexOf("px"))));
			this.font = font.substring(font.indexOf("px ")+3);
			this.minusSign.setFont(this.font, this.getFontHeight());
			this.multiplicationSign.setFont(this.font, this.getFontHeight());
			this.multiplicationSign.setSpace(this.getFontHeight() * 0.2d);

			for (View view : this.views) {
				view.setFont(font);
			}
			
			update();
		} else {
			this.setFontHeight(null);
			this.font = null;
		}
	}
	
	/**
	 * updates the view after changes
	 */
	@Override
	public void update() {
		Surface surface = new Surface();
		surface.setFont(this.getFont());
		
		Double signWidth = this.multiplicationSign.getWidth();
		Double bracketsWidth = surface.measureText("()");
		Double minusWidth = this.minusSign.getWidth();
		Double maxOverBaseline = 0d;
		
		this.setRectangle(new Rectangle());
		
		for (int i = 0; i < this.views.size(); i++) {
			View view = this.views.elementAt(i);
			this.setWidth(this.getWidth() + view.getWidth());
			
			if ((i > 0)&&(!(view instanceof VariableView))) this.setWidth(this.getWidth() + signWidth);
			
			if(view instanceof SumView) this.setWidth(this.getWidth() + bracketsWidth);
			
			Double overBaseline = 0d;
			
			if (view instanceof FractionView) {
				Double baselineOffset = ((FractionView) view).getDenominator().getHeight() - this.getFontHeight()/2 + FractionView.getSpace();
				overBaseline = view.getHeight() - baselineOffset;
				this.setBaselineOffset(Math.max(this.getBaselineOffset(), baselineOffset));			
			} else {
				this.setBaselineOffset(Math.max(this.getBaselineOffset(), view.getBaselineOffset()));
				overBaseline = view.getHeight() - view.getBaselineOffset();
			}
			maxOverBaseline = Math.max(maxOverBaseline, overBaseline);
		}
		
		this.setHeight(maxOverBaseline + this.getBaselineOffset());
		
		
		if(this.isNegative())
			this.setWidth(this.getWidth() + minusWidth);
		
	}
	
}
